import React from 'react';
import menus from 'menu';
import {
    Form,
    Input,
    Select,
    Cascader,
    Row,
    Col,
    Checkbox,
    Button,
    Modal,
    Table,
    Pagination,
    Radio,
    Icon,
    Collapse,
    Tree,
    TreeSelect
} from 'antd';
import filter from 'filter';
import util from 'util';


const FormItem = Form.Item;
const RadioGroup = Radio.Group;
const Option = Select.Option;
const CheckboxGroup = Checkbox.Group;
const Panel = Collapse.Panel;
const TreeNode = Tree.TreeNode;

//搜索面板
@Form.create()
class Search extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            roletypeChildren: [],
            userstatusChildren: [],
            usertypeChildren: []
        };
    }

    componentDidMount() {

        //角色类型下拉选项
        //var roletypedict = util.getSysDict("roletype"), roletypelist = [];
        //for (let i = 0; i < roletypedict.length; i++) {
        //    roletypelist.push(<Option key={roletypedict[i].itemkey}>{roletypedict[i].itemval}</Option>);
        //}
        //用户状态下拉选项
        var userstatusdict = util.getSysDict("userstatus"), userstatuslist = [];
        for (let i = 0; i < userstatusdict.length; i++) {
            userstatuslist.push(<Option key={userstatusdict[i].itemkey}>{userstatusdict[i].itemval}</Option>);
        }
        //用户类型下拉选项
        var usertypedict = util.getSysDict("usertype"), usertypelist = [];
        for (let i = 0; i < usertypedict.length; i++) {
            usertypelist.push(<Option key={usertypedict[i].itemkey}>{usertypedict[i].itemval}</Option>);
        }
        this.setState({
            userstatusChildren: userstatuslist,
            usertypeChildren: usertypelist
        });

    }

    search(e) {
        e.preventDefault();
        var {getFieldsValue} = this.props.form;
        console.log(getFieldsValue());
        var searchParams = getFieldsValue();
        this.props.search(searchParams);
    }

    reset(e) {
        e.preventDefault();
        var {resetFields} = this.props.form;
        resetFields();
    }

    render() {
        const {getFieldProps, getFieldValue} = this.props.form;
        var usertype = getFieldProps("usertype");
        var loginname = getFieldProps("loginname");
        var username = getFieldProps("username");
        var userstatus = getFieldProps("userstatus");
        var orgno = getFieldProps("orgno");
        return (
            <Collapse defaultActiveKey={['1']}>
                <Panel header="用户管理信息查询" key="1">
                    <Form inline className="ant-advanced-search-form">
                        <Row>
                            <Col span={12}>
                                <FormItem label="用户名称：">
                                    <Input
                                        {...username}
                                    />
                                </FormItem>
                            </Col>
                            <Col span={12}>
                                <FormItem label="登录名称：">
                                    <Input
                                        {...loginname}
                                    />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col span={24}>
                                <FormItem label="用户状态：">
                                    <Select {...userstatus} style={{width: 160}} placeholder="请选择">
                                        {this.state.userstatusChildren}
                                    </Select>
                                </FormItem>
                            </Col>
                        </Row>

                        <Row style={{textAlign: 'center'}}>
                            <Button className="btnMarginRright" type="primary" htmlType="submit"
                                    onClick={this.search.bind(this)}
                            >搜索</Button>
                            <Button htmlType="submit" onClick={this.reset.bind(this)}>重置</Button>
                        </Row>
                    </Form>
                </Panel>
            </Collapse>
        );
    }
}
//修改用户信息
@Form.create()
class UserForm extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            roletypeChildren: [],
            usertypeChildren: [],
            sexChildren: [],
            idtypeChildren: [],
        };
    }

    componentDidMount() {
        //角色类型下拉选项
        var roletypedict = util.getSysDict("roletype"), roletypelist = [];
        for (let i = 0; i < roletypedict.length; i++) {
            roletypelist.push(<Option key={roletypedict[i].itemkey}>{roletypedict[i].itemval}</Option>);
        }
        //用户类型下拉选项
        var usertypedict = util.getSysDict("usertype"), usertypelist = [];
        for (let i = 0; i < usertypedict.length; i++) {
            usertypelist.push(<Option key={usertypedict[i].itemkey}>{usertypedict[i].itemval}</Option>);
        }
        //性别下拉选项
        var sexdict = util.getSysDict("sex"), sexdictlist = [];
        for (let i = 0; i < sexdict.length; i++) {
            sexdictlist.push(<Option key={sexdict[i].itemkey}>{sexdict[i].itemval}</Option>);
        }
        //证件类型下拉选项
        var idtypedict = util.getSysDict("id_type"), idtypedictlist = [];
        for (let i = 0; i < idtypedict.length; i++) {
            idtypedictlist.push(<Option key={idtypedict[i].itemkey}>{idtypedict[i].itemval}</Option>);
        }
        var {setFieldsValue} = this.props.form;
        console.log(this.props.data);
        setFieldsValue(this.props.data);
        this.setState({
            roletypeChildren: roletypelist,
            usertypeChildren: usertypelist,
            sexChildren: sexdictlist,
            idtypeChildren: idtypedictlist
        });
    }

    onSelect(result) {
        console.log('组织', result);
        this.props.form.setFieldsValue({
            orgno: result
        })
    }

    checkTel(rule, value, callback) {
        const {validateFields} = this.props.form;
        const reg = /^\d{11}$/;
        if (value) {
            if (!reg.test(value)) {
                callback([new Error('请输入11位手机号！')]);
            } else {
                callback();
            }
        } else {
            callback();
        }
    }

    checkFaxno(rule, value, callback) {
        //const { validateFields } = this.props.form;
        //const reg = /^\d{7,8}$/;
        //if (value && !reg.test(value)) {
        //    callback([new Error('请输入7或8位号码！')]);
        //} else {
        //    callback();
        //}
    }

    checkIdno(rule, value, callback) {
        const {validateFields} = this.props.form;
        const reg = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
        if (value) {
            if (!reg.test(value)) {
                callback([new Error('请输入15或18位身份证号！')]);
            } else {
                callback();
            }
        } else {
            callback();
        }
    }


    render() {
        const {getFieldProps, getFieldValue, setFieldsValue} = this.props.form;
        // var usertype = getFieldProps("usertype", {
        //     rules: [
        //         {required: true, message: '用户类型必须输入'}
        //     ]
        // });
        var loginname = getFieldProps("loginname", {
            rules: [
                {required: true, whitespace: true, min: 0, message: '登录名称必须输入!'}
            ]
        });
        var username = getFieldProps("username", {
            rules: [
                {required: true, whitespace: true, min: 0, message: '用户名称必须输入!'}
            ]
        });
        var sex = getFieldProps("sex");
        var orgno = getFieldProps("orgno");
        var idtype = getFieldProps("idtype");
        var officeno = getFieldProps("officeno");
        var mobileno = getFieldProps("mobileno", {
            rules: [
                {validator: this.checkTel.bind(this)}
            ]
        });
        var email = getFieldProps("email", {
            rules: [
                {type: 'email', whitespace: true, message: '请输入正确的邮箱地址!'}
            ]
        });
        var idno = getFieldProps("idno", {
            rules: [
                {validator: this.checkIdno.bind(this)}
            ]
        });
        var faxno = getFieldProps("faxno");
        var homeno = getFieldProps("homeno");
        var postcode = getFieldProps("postcode");
        var address = getFieldProps("address");
        const formItemLayout = {
            labelCol: {span: 8},
            wrapperCol: {span: 14}
        };
        return (
            <Form horizontal form={this.props.form}>
                <Row gutter={16}>
                    <Col sm={24}>
                        <FormItem label="用户类型" labelCol={{span: 4}}
                                  wrapperCol={{span: 14}}
                        >

                            <span>管理台</span>
                        </FormItem>
                    </Col>
                    <Col sm={24} id="selectBodyEdit">
                        <FormItem
                            label="所属机构" required labelCol={{span: 4}}
                            wrapperCol={{span: 14}}
                        >
                            <OrgTreeSelect {...this.props.options}
                                           {...orgno}
                                           componentClass="EDIT"
                                           form={this.props.form}
                                           propsname="parentorgno"
                                           onSelect={this.onSelect.bind(this)}></OrgTreeSelect>
                        </FormItem>
                    </Col>
                    <Col sm={12}>
                        <FormItem
                            label="登录名称" required {...formItemLayout}
                        >

                            <Input type="text" disabled placeholder="请输入登录名称" {...loginname}/>
                        </FormItem>

                        <FormItem
                            label="证件类型" {...formItemLayout}
                        >
                            <Select
                                {...idtype}

                            >
                                {this.state.idtypeChildren}
                            </Select>
                        </FormItem>
                        <FormItem
                            label="办公电话" {...formItemLayout}
                        >

                            <Input type="tel" placeholder="请输入办公电话" {...officeno}/>
                        </FormItem>
                        <FormItem
                            label="移动电话" {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入移动电话" {...mobileno}/>
                        </FormItem>
                        <FormItem
                            label="电子邮箱" {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入电子邮箱" {...email}/>
                        </FormItem>
                    </Col>
                    <Col sm={12}>
                        <FormItem
                            label="用户名称" required hasFeedback {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入用户名称" {...username}/>
                        </FormItem>
                        <FormItem
                            label="性别" {...formItemLayout}
                        >

                            <Select{...sex}
                            >
                                {this.state.sexChildren}
                            </Select>
                        </FormItem>

                        <FormItem
                            label="证件号码" {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入证件号码" {...idno}/>
                        </FormItem>
                        <FormItem
                            label="传真" {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入传真" {...faxno}/>
                        </FormItem>
                        <FormItem
                            label="住宅电话" {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入住宅电话" {...homeno}/>
                        </FormItem>
                        <FormItem
                            label="邮编" {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入邮编" {...postcode}/>
                        </FormItem>
                    </Col>
                    <Col sm={24}>

                        <FormItem
                            label="地址" labelCol={{span: 4}}
                            wrapperCol={{span: 20}}
                        >

                            <Input type="text" placeholder="请输入地址" {...address}/>
                        </FormItem>
                    </Col>

                </Row>
            </Form>
        );
    }

    login() {
        this.props.form.validateFields((errors, values) => {
            if (!!errors) {
                return;
            }
            console.log(values);
            this.props.login(values);
        })
    }

}
@Form.create()
class SetRole extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            roleChildren: [],
            defaultValue: []
        };
    }

    componentDidMount() {
        //console.log("@@@@@@@@@@@@@@@@@@@@@@@", this.props.userid);
        //var userid = this.props.userid;
        ////获取角色信息
        //util.httpRequest({
        //    type: "POST",
        //    url: 'user020107.json',
        //    params: {userid: userid},
        //    success: function (result) {
        //        console.log(result.roleids);
        //        var roleList = result.rows, roleChildren = [];
        //        for (let i = 0; i < roleList.length; i++) {
        //            roleChildren.push({value: roleList[i].roleid, label: roleList[i].rolename});
        //
        //            for (let j = 0; j < result.roleids.length; j++) {
        //                if (roleList[i].roleid == result.roleids[j]) {
        //                    roleChildren[i].defaultValue = result.roleids[j];
        //                    roleChildren[i].checked = true;
        //
        //                } else {
        //                    roleChildren[i].defaultValue = "";
        //                    roleChildren[i].checked = true;
        //
        //
        //                }
        //            }
        //        }
        //        console.log(roleChildren);
        //        this.setState({
        //            checkComponentList: <CheckboxGroup defaultValue={result.roleids} options={roleChildren}
        //                                               onChange={this.onChange.bind(this)}/>
        //        });
        //    }.bind(this)
        //});
        var result = this.props.roleData;
        var roleChildren = [];
        var roleList = this.props.roleData.roles_op;
        var roles_user = this.props.roleData.roles_user;
        let selectedids = [];
        for (let {roleid} of roles_user) {
            //if(roleid!="1"){
            selectedids.push(roleid);
            // }
        }
        console.log("list:", roleList, "");
        // 2134wqerq
        for (let i = 0; i < roleList.length; i++) {
            // if(roleList[i].roleid!="1"){
            roleChildren.push({value: roleList[i].roleid, label: roleList[i].rolename});
            // }

            for (let j = 0; j < roles_user.length; j++) {
                if (roleList[i].roleid == roles_user[j].roleid) {
                    roleChildren[i].defaultValue = roles_user[j].roleid;
                    roleChildren[i].checked = true;
                } else {
                    roleChildren[i].defaultValue = "";
                    roleChildren[i].checked = true;
                }
            }
        }
        console.log(roleChildren);
        this.setState({
            checkComponentList: <CheckboxGroup defaultValue={selectedids} options={roleChildren}
                                               onChange={this.onChange.bind(this)}/>
        });
    }

    onChange(checkedValues) {
        console.log('checked = ', checkedValues);
        this.props.onChange(checkedValues)
    }

    render() {
        const {getFieldProps, getFieldValue} = this.props.form;
        console.log(this.state.defaultValue);
        return (
            <Form horizontal form={this.props.form}>
                <FormItem
                    label="所属角色:"
                    labelCol={{span: 6}}
                    wrapperCol={{span: 18}}
                >
                    {this.state.checkComponentList}
                </FormItem>
            </Form>
        )


    }

    login() {
        this.props.form.validateFields((errors, values) => {
            if (!!errors) {
                return;
            }
            console.log(values);
            this.props.login(values);
        })
    }
}
@Form.create()
class SetPwd extends React.Component {
    checkPass(rule, value, callback) {
        const {validateFields} = this.props.form;
        const reg = /(?=.*?[a-zA-Z])(?=.*?[0-9])[a-zA-Z0-9]{6,18}$/;
        if (value && !reg.test(value)) {
            callback([new Error('请输入6-18位数字加字母组合密码')]);
        } else {
            callback();
        }

    }

    checkPass2(rule, value, callback) {
        const {getFieldValue} = this.props.form;
        if (value && value !== getFieldValue('password')) {
            callback('两次输入密码不一致！');
        } else {
            callback();
        }
    }

    render() {
        const {getFieldProps, getFieldValue} = this.props.form;

        const password = getFieldProps('password', {
            rules: [
                {required: true, whitespace: true, message: '请填写密码'},
                {validator: this.checkPass.bind(this)},
            ],
            onChange: (e) => {
                console.log('你的密码就是这样被盗的：', e.target.value);
            },
        });
        const sure_password = getFieldProps('sure_password', {
            rules: [{
                required: true,
                whitespace: true,
                message: '请再次输入密码',
            }, {
                validator: this.checkPass2.bind(this),
            }],
        });
        const formItemLayout = {
            labelCol: {span: 6},
            wrapperCol: {span: 18}
        };

        return (
            <Form horizontal form={this.props.form}>
                <Row>
                    <Col span={18}>
                        <FormItem
                            label="密码"
                            {...formItemLayout}
                            required
                        >

                            <Input type="password" placeholder="请输入密码" {...password}/>
                        </FormItem>
                    </Col>
                    <Col span={18}>
                        <FormItem
                            label="确认密码"
                            {...formItemLayout}
                            required
                        >

                            <Input type="password" placeholder="请输入确认密码" {...sure_password}/>
                        </FormItem>
                    </Col>
                </Row>
            </Form>
        )
    }

    login() {
        this.props.form.validateFields((errors, values) => {
            if (!!errors) {
                return;
            }
            console.log(values);
            this.props.login(values);
        })
    }
}
@Form.create()
class AddForm extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            roletypeChildren: [],
            usertypeChildren: [],
            sexChildren: [],
            idtypeChildren: [],
        };
    }

    componentDidMount() {
        //角色类型下拉选项
        var roletypedict = util.getSysDict("roletype"), roletypelist = [];
        for (let i = 0; i < roletypedict.length; i++) {
            roletypelist.push(<Option key={roletypedict[i].itemkey}>{roletypedict[i].itemval}</Option>);
        }
        //用户类型下拉选项
        var usertypedict = util.getSysDict("usertype"), usertypelist = [];
        for (let i = 0; i < usertypedict.length; i++) {
            usertypelist.push(<Option key={usertypedict[i].itemkey}>{usertypedict[i].itemval}</Option>);
        }
        //性别下拉选项
        var sexdict = util.getSysDict("sex"), sexdictlist = [];
        for (let i = 0; i < sexdict.length; i++) {
            sexdictlist.push(<Option key={sexdict[i].itemkey}>{sexdict[i].itemval}</Option>);
        }
        //证件类型下拉选项
        var idtypedict = util.getSysDict("id_type"), idtypedictlist = [];
        for (let i = 0; i < idtypedict.length; i++) {
            idtypedictlist.push(<Option key={idtypedict[i].itemkey}>{idtypedict[i].itemval}</Option>);
        }
        this.setState({
            roletypeChildren: roletypelist,
            usertypeChildren: usertypelist,
            sexChildren: sexdictlist,
            idtypeChildren: idtypedictlist
        });
    }

    checkPass(rule, value, callback) {
        const {validateFields} = this.props.form;
        const reg = /(?=.*?[a-zA-Z])(?=.*?[0-9])[a-zA-Z0-9]{6,18}$/;
        if (value && !reg.test(value)) {
            callback([new Error('请输入6-18位数字加字母组合密码')]);
        } else {
            callback();
        }

    }

    checkPass2(rule, value, callback) {
        const {getFieldValue} = this.props.form;
        if (value && value !== getFieldValue('password')) {
            callback('两次输入密码不一致!!!');
        } else {
            callback();
        }
    }

    onSelect(result) {
        console.log('组织', result);
        this.props.form.setFieldsValue({
            orgno: result
        })
    }

    checkTel(rule, value, callback) {
        const {validateFields} = this.props.form;
        const reg = /^\d{11}$/;
        if (value) {
            if (!reg.test(value)) {
                callback([new Error('请输入11位手机号！')]);
            } else {
                callback();
            }
        } else {
            callback();
        }
    }

    checkFaxno(rule, value, callback) {
        //const { validateFields } = this.props.form;
        //const reg = /^\d{7,8}$/;
        //if (value && !reg.test(value)) {
        //    callback([new Error('请输入7或8位号码！')]);
        //} else {
        //    callback();
        //}
    }

    checkIdno(rule, value, callback) {
        console.log(value);
        const {validateFields} = this.props.form;
        const reg = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
        if (value && !reg.test(value)) {
            callback([new Error('请输入15或18位身份证号！')]);
        } else {
            callback();
        }
    }

    // clear(){
    //     console.log( this.props.form)
    //         var {setFieldsValue} = this.props.form;
    //         setFieldsValue({idno:""});
    // }
    checkPrime(rule, value, callback) {
        if (this.props.form.getFieldProps("idtype").value != "A") {
            callback();
        }
        // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X---
        var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if (reg.test(value) === false) {
            callback(new Error('请输入合法的身份证号码'));
        } else {
            callback();
        }
    }

    render() {
        const {getFieldProps, getFieldValue} = this.props.form;

        // var usertype = getFieldProps("usertype", {
        //     rules: [
        //         {required: true, message: '用户类型必须输入'}
        //     ]
        // });
        var loginname = getFieldProps("loginname", {
            rules: [
                {required: true,
                    message: '请输入登录名称',
                }
            ]
        });
        const password = getFieldProps('password', {
            rules: [
                {required: true, whitespace: true, message: '请填写密码'},
                {validator: this.checkPass.bind(this)},
            ]
        });
        const sure_password = getFieldProps('sure_password', {
            rules: [{
                required: true,
                whitespace: true,
                message: '请再次输入密码',
            }, {
                validator: this.checkPass2.bind(this),
            }],
        });
        var username = getFieldProps("username", {
            rules: [
                {required: true, message: '用户名称必须输入'}
            ]
        });
        var sex = getFieldProps("sex");
        var orgno = getFieldProps("orgno", {
            rules: [
                {required: true, message: '所属机构必须输入'}
            ]
        });
        var idtype = getFieldProps("idtype");
        var officeno = getFieldProps("officeno");
        var mobileno = getFieldProps("mobileno", {
            rules: [
                {validator: this.checkTel.bind(this)}

            ]
        });
        var email = getFieldProps("email", {
            rules: [
                {type: 'email', message: '请输入正确的邮箱地址!'}
            ]
        });
        var idno = getFieldProps("idno", {

            rules: [
                {validator: this.checkPrime.bind(this)},
            ],
            //rules: [
            //    {validator: this.checkIdno.bind(this)}
            //
            //]
        });
        var faxno = getFieldProps("faxno");
        var homeno = getFieldProps("homeno");
        var postcode = getFieldProps("postcode");
        var address = getFieldProps("address");
        const formItemLayout = {
            labelCol: {span: 8},
            wrapperCol: {span: 14}
        };
        return (
            <Form horizontal form={this.props.form}>
                <Row gutter={16}>
                    <Col sm={24}>
                        <FormItem label="用户类型" labelCol={{span: 4}}
                                  wrapperCol={{span: 14}}
                        >
                            <span>管理台</span>
                        </FormItem>
                    </Col>
                    <Col sm={24} id="selectBodyAdd">
                        <FormItem label="所属机构" labelCol={{span: 4}}
                                  wrapperCol={{span: 14}}

                        >
                            <OrgTreeSelect style={{top: "35px"}} {...this.props.options}
                                           {...orgno}
                                           componentClass="ADD"
                                           form={this.props.form}
                                           propsname="parentorgno"
                                           onSelect={this.onSelect.bind(this)}></OrgTreeSelect>

                        </FormItem>
                    </Col>
                    <Col sm={12}>


                        <div style={{
                            "z-dindex": "-10",
                            "position": "absolute",
                            "width": "1px",
                            "height": "1px",
                            "right": "-10000"
                        }}>
                            <Input type="text"/><Input type="password"/>
                        </div>


                        <FormItem
                            label="登录名称" required {...formItemLayout}
                        >
                            <Input type="text" placeholder="请输入登录名称" {...loginname}/>
                        </FormItem>
                        <FormItem
                            label="密码" hasFeedback required {...formItemLayout}
                        >

                            <Input type="password" placeholder="请输入密码" {...password}/>
                        </FormItem>

                        <FormItem
                            label="证件类型" {...formItemLayout}
                        >

                            <Select
                                {...idtype}
                            >
                                {this.state.idtypeChildren}
                            </Select>
                        </FormItem>
                        <FormItem hasFeedback
                                  label="办公电话" {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入办公电话" {...officeno}/>
                        </FormItem>
                        <FormItem
                            label="移动电话" {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入移动电话" {...mobileno}/>
                        </FormItem>
                        <FormItem
                            label="电子邮箱" {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入电子邮箱" {...email}/>
                        </FormItem>
                    </Col>
                    <Col sm={12}>
                        <FormItem
                            label="用户名称" required hasFeedback {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入用户名称" {...username}/>
                        </FormItem>
                        <FormItem hasFeedback
                                  label="确认密码" required {...formItemLayout}
                        >

                            <Input type="password" placeholder="请输入确认密码" {...sure_password}/>
                        </FormItem>
                        <FormItem
                            label="性别" {...formItemLayout}
                        >

                            <Select{...sex}
                            >
                                {this.state.sexChildren}
                            </Select>
                        </FormItem>

                        <FormItem
                            label="证件号码" {...formItemLayout}
                        >

                            <Input
                                type="text" placeholder="请输入证件号码" {...idno}/>
                        </FormItem>
                        <FormItem
                            label="传真" {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入传真" {...faxno}/>
                        </FormItem>
                        <FormItem
                            label="住宅电话" {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入住宅电话" {...homeno}/>
                        </FormItem>
                        <FormItem
                            label="邮编" {...formItemLayout}
                        >

                            <Input type="text" placeholder="请输入邮编" {...postcode}/>
                        </FormItem>
                    </Col>
                    <Col sm={24}>

                        <FormItem
                            label="地址" labelCol={{span: 4}}
                            wrapperCol={{span: 20}}
                        >

                            <Input type="text" placeholder="请输入地址" {...address}/>
                        </FormItem>
                    </Col>

                </Row>
            </Form>
        );
    }

    login() {
        this.props.form.validateFields((errors, values) => {
            if (!!errors) {
                return;
            }
            console.log(values);
            this.props.login(values);
        })
    }

}
function generateTreeNodes(treeNode) {
    const arr = [];
    const key = treeNode.props.eventKey;
    console.log("treenodekey:" + key);
    for (let i = 0; i < 3; i++) {
        arr.push({name: `leaf ${key}-${i}`, key: `${key}-${i}`});
    }
    return arr;
}

function setLeaf(treeData, curKey, level) {
    console.log("@@@@@@@@@@@@@@@", treeData)
    const loopLeaf = (data, lev) => {
        const l = lev - 1;
        data.forEach((item) => {
            if ((item.key.length > curKey.length) ? item.key.indexOf(curKey) !== 0 :
                    curKey.indexOf(item.key) !== 0) {
                return;
            }
            if (item.children) {
                loopLeaf(item.children, l);
            } else if (l < 1) {
                item.isLeaf = true;
            }
        });
    };
    loopLeaf(treeData, level + 1);
}

function getNewTreeData(treeData, curKey, child, level) {
    const loop = (data) => {
        //if (level < 1 || curKey.length - 3 > level * 2) return;
        data.forEach((item) => {
            var cukey = curKey.split(",")[0];
            var itemkey = item.orgid;
            if (cukey.indexOf(itemkey) === 0) {
                if (item.children) {
                    loop(item.children);
                } else {
                    item.children = child;
                }
            }
        });
    };
    loop(treeData);
    setLeaf(treeData, curKey, level);
}
//组织树
const OrgTree = React.createClass({
    getInitialState() {
        return {
            treeData: [],
        };
    },
    componentDidMount() {
        util.httpRequest({
            type: "POST", url: "org010101.json", params: {"parentorgno": ""}, success: function (result) {
                result.rows.forEach(function (i) {
                    if (i.isleaf == "true") {
                        i.isLeaf = true;
                    }
                    else {
                        i.isLeaf = false;
                    }
                });

                console.log(result.rows);
                this.setState({
                    treeData: result.rows
                });
            }.bind(this)
        });
    },
    onSelect(info) {
        // 查询用户
        if (info.length == 0) {
        } else {
            util.httpRequest({
                type: "POST",
                url: 'user020101.json',
                params: {page: "1", rows: "10", orgno: info[0].split(",")[1]},
                success: function (result) {
                    console.log(result);
                    this.props.onSelect(result, info[0]);
                }.bind(this)
            });
        }
    },
    onLoadData(treeNode) {
        return new Promise((resolve) => {
            const key = treeNode.props.eventKey;
            var orgno = key.split(",")[1];
            console.log("nodekey:" + key);
            util.httpRequest({
                type: "POST",
                url: "org010101.json",
                params: {orgno: "", orgname: "", parentorgno: orgno},
                success: function (result) {
                    console.log("orgtree");
                    console.log(result);
                    if (result.rows) {
                        result.rows.forEach(function (i) {
                            if (i.isleaf == "true") {
                                i.isLeaf = true;
                            }
                            else {
                                i.isLeaf = false;
                            }
                        });

                        const treeData = [...this.state.treeData];
                        getNewTreeData(treeData, treeNode.props.eventKey, result.rows, 2);
                        this.setState({treeData});
                        resolve();
                    }
                }.bind(this)
            });

        });
    },
    render() {
        //const {getFieldProps} =this.props.form;
        const loop = data => data.map((item) => {
            if (item.children) {
                return <TreeNode title={item.orgname} key={item.orgid + "," + item.orgno + "," + item.orgname}
                                 value={item.orgno}>{loop(item.children)}</TreeNode>;
            }
            return <TreeNode title={item.orgname} key={item.orgid + "," + item.orgno + "," + item.orgname}
                             isLeaf={item.isLeaf}
                             value={item.orgno}
                             disabled={item.key === '0-0-0'}/>;
        });
        const treeNodes = loop(this.state.treeData);
        return (
            <Tree onSelect={this.onSelect} loadData={this.onLoadData}
                  style={{width: "200px"}}>
                {treeNodes}
            </Tree>
        );
    }
});
//下拉组织树

const OrgTreeSelect = React.createClass({

    getInitialState() {
        return {
            treeData: []
        };
    },

    initTreeData(){
        return new Promise((resolve) => {
            const {orgname, orgno, parentorgno} = util.getLocalObj("userInfo");
            util.httpRequest({
                type: "POST", url: "org010101.json", params: {
                    orgname: "",
                    orgno: "", "parentorgno": ""
                }, success: function (result) {
                    result.rows.forEach(function (i) {
                        if (i.isleaf == "true") {
                            i.isLeaf = true;
                        }
                        else {
                            i.isLeaf = false;
                        }
                    });
                    resolve(result.rows);
                }.bind(this)
            });
        })
    },

    componentDidMount() {

        console.log(this)

        this.initTreeData().then(function (data) {
            this.treeData = data;
        }.bind(this));

        if (this.props.orgid && this.props.orgno && this.props.orgname) {
            this.setState({
                treeData: [{orgid: this.props.orgid, orgno: this.props.orgno, orgname: this.props.orgname}]
            });
        }
    },
    onSelect(info) {
        console.log('selected31', info);
        this.props.onSelect(info)
    },
    onLoadData(treeNode) {
        return new Promise((resolve) => {
            const key = treeNode.props.eventKey;
            var orgno = key.split(",")[1];
            console.log("nodekey:" + key);
            util.httpRequest({
                type: "POST", url: "org010101.json", params: {
                    orgname: "",
                    orgno: "", "parentorgno": orgno
                }, success: function (result) {
                    console.log("orgtree");
                    console.log(result);
                    if (result.rows) {
                        result.rows.forEach(function (i) {
                            if (i.isleaf == "true") {
                                i.isLeaf = true;
                            }
                            else {
                                i.isLeaf = false;
                            }
                        });

                        const treeData = [...this.state.treeData];
                        getNewTreeData(treeData, treeNode.props.eventKey, result.rows, 2);
                        this.setState({treeData});
                        resolve();
                    }
                }.bind(this)
            });

        });
    },
    onClick(){
        //1qweqwqe
        this.props.form.setFields({
            orgno: {
                value: this.props.orgname
            },
        });
        this.setState({
            treeData: this.treeData
        })
    },
    render() {
        const {getFieldProps} =this.props.form;
        const loop = data => data.map((item) => {
            if (item.children) {
                return <TreeNode title={item.orgname} key={item.orgid + "," + item.orgno + "," + item.orgname}
                                 value={item.orgno + "," + item.orgname + "," + item.orgid}>{loop(item.children)}</TreeNode>;
            }
            return <TreeNode title={item.orgname} key={item.orgid + "," + item.orgno + "," + item.orgname}
                             isLeaf={item.isLeaf}
                             value={item.orgno + "," + item.orgname + "," + item.orgid}
                             disabled={item.key === '0-0-0'}/>;
        });
        const treeNodes = loop(this.state.treeData);
        return (
            <TreeSelect
                getPopupContainer={() => document.getElementById(this.props.componentClass == "ADD" ? "selectBodyAdd" : "selectBodyEdit")} {...getFieldProps('orgno')}
                onClick={this.onClick} onSelect={this.onSelect}
                loadData={this.onLoadData}>
                {treeNodes}
            </TreeSelect>
        );
    }
});

export {Search, UserForm, SetRole, SetPwd, AddForm, OrgTree, OrgTreeSelect}
